package com.bjb.vr.operation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.ResBundleAo;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptProdBundleService;
import com.bjb.vr.common.service.ResBundleService;
import com.bjb.vr.common.vo.ResBundleDetailVo;
import com.bjb.vr.common.vo.ResBundleVo;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description: 资源包管理
 * @author: sea
 * @create: 2022-11-28 14:39
 **/
@RestController
@RequestMapping("/api/operation/v1")
public class OptProdBundleController {

    @Resource
    private OptProdBundleService optProdBundleService;




    /**
     * 查询资源包列表
     *
     * @return
     */
    @PostMapping("/queryBundleList")
    public BaseResult<IPage<ResBundleVo>> queryBundleList(@RequestBody ResBundleAo resBundleAo) {
        IPage<ResBundleVo> resBundleVos = optProdBundleService.queryBundleList(resBundleAo);
        return CommonResult.Ok().setResult(resBundleVos);
    }

    /**
     * 查询资源包--单条
     *
     * @return
     */
    @PostMapping("/queryResBundleOne")
    public BaseResult queryResBundleOne(@RequestBody ResBundleAo resBundleAo) {
        ResBundleVo resBundleVo = optProdBundleService.queryResBundleOne(resBundleAo);
        return CommonResult.Ok().setResult(resBundleVo);
    }

    /**
     * 新建资源包
     *
     * @return
     */
    @PostMapping("/addResBundle")
    public BaseResult addResBundle(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getBundleType(), "资源包类型不能为空");
        // 查询资源包名称是否存在
        boolean exist = optProdBundleService.queryResBundle(resBundleAo);
        if (exist) {
            return CommonResult.Fail("该资源包已存在！");
        }
        // 新建资源包
        boolean success = optProdBundleService.addResBundle(resBundleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 编辑资源包
     *
     * @return
     */
    @PostMapping("/updateResBundle")
    public BaseResult updateResBundle(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getId(), "资源包ID不能为空");
        // 新建资源包
        boolean success = optProdBundleService.updateResBundle(resBundleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 查询资源包详情
     *
     * @param resBundleAo
     * @return
     * @throws Exception
     */
    @PostMapping("/queryResBundleDetail")
    public BaseResult<IPage<ResBundleDetailVo>> queryResBundleDetail(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getId(), "资源包ID不能为空");
        IPage<ResBundleDetailVo> resBundleDetail = optProdBundleService.queryResBundleDetail(resBundleAo);
        return CommonResult.Ok().setResult(resBundleDetail);
    }

    /**
     * 查询当前资源包以外的其他资源内容
     *
     * @param resBundleAo
     * @return
     * @throws Exception
     */
    @PostMapping("/queryOtherResBundleDetail")
    public BaseResult queryOtherResBundleDetail(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getId(), "资源包ID不能为空");
        IPage<ResBundleDetailVo> resPackageDetailVos = optProdBundleService.queryOtherResBundleDetail(resBundleAo);
        return CommonResult.Ok().setResult(resPackageDetailVos);
    }

    /**
     * 资源包添加新资源
     *
     * @param resBundleAo
     * @return
     */
    @PostMapping("/addResBundleDetail")
    public BaseResult addResBundleDetail(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getId(), "资源包ID不能为空");
        boolean success = optProdBundleService.addResBundleDetail(resBundleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 资源包删除已有资源
     *
     * @param resBundleAo
     * @return
     */
    @PostMapping("/removeResBundleDetail")
    public BaseResult removeResBundleDetail(@RequestBody ResBundleAo resBundleAo) {
        Assert.notNull(resBundleAo.getId(), "资源包ID不能为空");
        Assert.notNull(resBundleAo.getResIds(), "资源内容ID不能为空");
        boolean success = optProdBundleService.removeResBundleDetail(resBundleAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }
}
